 /*
    MegaShorter, the ultimate url-shorting tool
    Copyright (C) 2011  Ivan I. Kolesnikov

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "tweaktkshorter.h"

QSharedPointer<Shorter> TweakTkShorterCreator::create(QSharedPointer<QSettings> settings)
{
    return QSharedPointer<Shorter>(new TweakTkShorter(settings), &QObject::deleteLater);
}

TweakTkShorter::TweakTkShorter(QSharedPointer<QSettings> s, QObject *parent) :
        Shorter(s, parent)
{
    setName("TweaK.tk");
    setShorterSite(QUrl("http://tweak.tk"));
}

void TweakTkShorter::doShortLink(const QUrl &url)
{
    QUrl reqUrl("http://api.dot.tk/tweak/shorten");

    reqUrl.addQueryItem("long",url.toString());

    QNetworkReply *r = manager()->get(QNetworkRequest(reqUrl));
    applyAttribute(r, url);
}

void TweakTkShorter::doFinish(QNetworkReply *reply)
{
    QString s(reply->readAll());

    if (reply->error() != QNetworkReply::NoError)
        emit done(Shorter::Error,"","Error while processing url.");
    else
        emit done(Shorter::Ok,getAttribute(reply),s.split("\n")[0].trimmed());
}
